-- tentacle.lua

tentacle_width = 8
tentacle_overlap = tentacle_width
tentacle_high_length = 125
tentacle_high_mid_length = 115
tentacle_mid_length = 120
tentacle_low_mid_length = 90
tentacle_low_length = 100
tentacle_joint_limit = math.pi / 6

tentacle_density = 0.75
tentacle_linear_damping = 0.2

Tentacle = {
   bodies = nil,
   shapes = nil,
   joints = nil,
   textures = nil,

   fade = 1, -- fades from normal texture (0 for from texture, 1 for to texture)
   from_tex = 0, to_tex = 0,
   from_tint_r = 255, from_tint_g = 255, from_tint_b = 255, -- tint
   to_tint_r = 255, to_tint_g = 255, to_tint_b = 255, -- tint
}

function Tentacle:new( o )
   o = o or {}
   setmetatable( o, self )
   self.__index = self
   return o
end

function Tentacle:init( world, parent, limb_anchor_x, limb_anchor_y )
   self.bodies = {}
   self.shapes = {}
   self.joints = {}
   self.textures = {}

   self.bodies.high = nil
   self.bodies.high_mid = nil
   self.bodies.mid = nil
   self.bodies.low_mid = nil
   self.bodies.low = nil
   self.shapes.high = nil
   self.shapes.high_mid = nil
   self.shapes.mid = nil
   self.shapes.low_mid = nil
   self.shapes.low = nil
   self.joints.high = nil
   self.joints.high_mid = nil
   self.joints.low_mid = nil
   self.joints.low = nil

   self.textures.high                     = {}
   self.textures.high[0]                  = love.graphics.newImage( "images/Octopus/tenticles/Tent_02_00.png" )
   self.textures.high[tile_id["sand"]]    = love.graphics.newImage( "images/Octopus/tenticles/Tent_03_00.png" )
   self.textures.high[tile_id["gravel"]]  = love.graphics.newImage( "images/Octopus/tenticles/Tent_04_00.png" )
   self.textures.high[tile_id["coral_g"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_05_00.png" )
   self.textures.high[tile_id["coral_p"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_06_00.png" )
   self.textures.high[tile_id["coral_r"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_07_00.png" )
   self.textures.high[tile_id["rocks"]]   = love.graphics.newImage( "images/Octopus/tenticles/Tent_08_00.png" )

   self.textures.high_mid                     = {}
   self.textures.high_mid[0]                  = love.graphics.newImage( "images/Octopus/tenticles/Tent_02_01.png" )
   self.textures.high_mid[tile_id["sand"]]    = love.graphics.newImage( "images/Octopus/tenticles/Tent_03_01.png" )
   self.textures.high_mid[tile_id["gravel"]]  = love.graphics.newImage( "images/Octopus/tenticles/Tent_04_01.png" )
   self.textures.high_mid[tile_id["coral_g"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_05_01.png" )
   self.textures.high_mid[tile_id["coral_p"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_06_01.png" )
   self.textures.high_mid[tile_id["coral_r"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_07_01.png" )
   self.textures.high_mid[tile_id["rocks"]]   = love.graphics.newImage( "images/Octopus/tenticles/Tent_08_01.png" )

   self.textures.mid                     = {}
   self.textures.mid[0]                  = love.graphics.newImage( "images/Octopus/tenticles/Tent_02_02.png" )
   self.textures.mid[tile_id["sand"]]    = love.graphics.newImage( "images/Octopus/tenticles/Tent_03_02.png" )
   self.textures.mid[tile_id["gravel"]]  = love.graphics.newImage( "images/Octopus/tenticles/Tent_04_02.png" )
   self.textures.mid[tile_id["coral_g"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_05_02.png" )
   self.textures.mid[tile_id["coral_p"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_06_02.png" )
   self.textures.mid[tile_id["coral_r"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_07_02.png" )
   self.textures.mid[tile_id["rocks"]]   = love.graphics.newImage( "images/Octopus/tenticles/Tent_08_02.png" )

   self.textures.low_mid                     = {}
   self.textures.low_mid[0]                  = love.graphics.newImage( "images/Octopus/tenticles/Tent_02_03.png" )
   self.textures.low_mid[tile_id["sand"]]    = love.graphics.newImage( "images/Octopus/tenticles/Tent_03_03.png" )
   self.textures.low_mid[tile_id["gravel"]]  = love.graphics.newImage( "images/Octopus/tenticles/Tent_04_03.png" )
   self.textures.low_mid[tile_id["coral_g"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_05_03.png" )
   self.textures.low_mid[tile_id["coral_p"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_06_03.png" )
   self.textures.low_mid[tile_id["coral_r"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_07_03.png" )
   self.textures.low_mid[tile_id["rocks"]]   = love.graphics.newImage( "images/Octopus/tenticles/Tent_08_03.png" )

   self.textures.low                     = {}
   self.textures.low[0]                  = love.graphics.newImage( "images/Octopus/tenticles/Tent_02_04.png" )
   self.textures.low[tile_id["sand"]]    = love.graphics.newImage( "images/Octopus/tenticles/Tent_03_04.png" )
   self.textures.low[tile_id["gravel"]]  = love.graphics.newImage( "images/Octopus/tenticles/Tent_04_04.png" )
   self.textures.low[tile_id["coral_g"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_05_04.png" )
   self.textures.low[tile_id["coral_p"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_06_04.png" )
   self.textures.low[tile_id["coral_r"]] = love.graphics.newImage( "images/Octopus/tenticles/Tent_07_04.png" )
   self.textures.low[tile_id["rocks"]]   = love.graphics.newImage( "images/Octopus/tenticles/Tent_08_04.png" )

	-- high segment
	local body_x = limb_anchor_x - tentacle_high_length / 2
	local body_y = limb_anchor_y
	self.bodies.high = love.physics.newBody( world, body_x, body_y )
	self.shapes.high = love.physics.newRectangleShape( self.bodies.high, 0, 0,
                                                       tentacle_high_length, tentacle_width )
    self.shapes.high:setDensity( tentacle_density )
    self.bodies.high:setLinearDamping( tentacle_linear_damping )
    self.shapes.high:setCategory( 4 )
    self.shapes.high:setMask( 4 )
    self.shapes.high:setData( parent )
	self.bodies.high:setMassFromShapes()
    self.bodies.high:setAllowSleeping( false )

	-- high mid segment
	body_x = (limb_anchor_x -
              tentacle_high_length + tentacle_overlap -
              tentacle_high_mid_length / 2)
	body_y = limb_anchor_y
	self.bodies.high_mid = love.physics.newBody( world, body_x, body_y )
	self.shapes.high_mid = love.physics.newRectangleShape( self.bodies.high_mid, 0, 0,
                                                           tentacle_high_mid_length, tentacle_width )
    self.shapes.high_mid:setDensity( tentacle_density )
    self.bodies.high_mid:setLinearDamping( tentacle_linear_damping )
    self.shapes.high_mid:setCategory( 4 )
    self.shapes.high_mid:setMask( 4 )
    self.shapes.high_mid:setData( parent )
	self.bodies.high_mid:setMassFromShapes()
    self.bodies.high_mid:setAllowSleeping( false )

	-- mid segment
	body_x = (limb_anchor_x -
              tentacle_high_length + tentacle_overlap -
              tentacle_high_mid_length + tentacle_overlap -
              tentacle_mid_length / 2)
	body_y = limb_anchor_y
	self.bodies.mid = love.physics.newBody( world, body_x, body_y )
	self.shapes.mid = love.physics.newRectangleShape( self.bodies.mid, 0, 0,
                                                      tentacle_mid_length, tentacle_width )
    self.shapes.mid:setDensity( tentacle_density )
    self.bodies.mid:setLinearDamping( tentacle_linear_damping )
    self.shapes.mid:setCategory( 4 )
    self.shapes.mid:setMask( 4 )
    self.shapes.mid:setData( parent )
	self.bodies.mid:setMassFromShapes()
    self.bodies.mid:setAllowSleeping( false )

	-- low mid segment
	body_x = (limb_anchor_x -
              tentacle_high_length + tentacle_overlap -
              tentacle_high_mid_length + tentacle_overlap -
              tentacle_mid_length + tentacle_overlap -
              tentacle_low_mid_length / 2)
	body_y = limb_anchor_y
	self.bodies.low_mid = love.physics.newBody( world, body_x, body_y )
	self.shapes.low_mid = love.physics.newRectangleShape( self.bodies.low_mid, 0, 0,
                                                          tentacle_low_mid_length, tentacle_width )
    self.shapes.low_mid:setDensity( tentacle_density )
    self.bodies.low_mid:setLinearDamping( tentacle_linear_damping )
    self.shapes.low_mid:setCategory( 4 )
    self.shapes.low_mid:setMask( 4 )
    self.shapes.low_mid:setData( parent )
	self.bodies.low_mid:setMassFromShapes()
    self.bodies.low_mid:setAllowSleeping( false )

	-- low segment
	body_x = (limb_anchor_x -
              tentacle_high_length + tentacle_overlap -
              tentacle_high_mid_length + tentacle_overlap -
              tentacle_mid_length + tentacle_overlap -
              tentacle_low_mid_length + tentacle_overlap -
              tentacle_low_length / 2)
	body_y = limb_anchor_y
	self.bodies.low = love.physics.newBody( world, body_x, body_y )
	self.shapes.low = love.physics.newRectangleShape( self.bodies.low, 0, 0,
                                                      tentacle_low_length, tentacle_width )
    self.shapes.low:setDensity( tentacle_density )
    self.bodies.low:setLinearDamping( tentacle_linear_damping )
    self.shapes.low:setCategory( 4 )
    self.shapes.low:setMask( 4 )
    self.shapes.low:setData( parent )
	self.bodies.low:setMassFromShapes()
    self.bodies.low:setAllowSleeping( false )

	-- high joint
	local joint_x = (limb_anchor_x -
                     tentacle_high_length + tentacle_overlap / 2)
	local joint_y = limb_anchor_y
	self.joints.high = love.physics.newRevoluteJoint( self.bodies.high,
                                                      self.bodies.high_mid,
                                                      joint_x, joint_y )
	self.joints.high:setLimitsEnabled( true )
	self.joints.high:setLimits( -tentacle_joint_limit, tentacle_joint_limit )

	-- high mid joint
	joint_x = (limb_anchor_x -
               tentacle_high_length + tentacle_overlap -
               tentacle_high_mid_length + tentacle_overlap / 2)
	joint_y = limb_anchor_y
	self.joints.high_mid = love.physics.newRevoluteJoint( self.bodies.high_mid,
                                                          self.bodies.mid,
                                                          joint_x, joint_y )
	self.joints.high_mid:setLimitsEnabled( true )
	self.joints.high_mid:setLimits( -tentacle_joint_limit, tentacle_joint_limit )

	-- low mid joint
	joint_x = (limb_anchor_x -
               tentacle_high_length + tentacle_overlap -
               tentacle_high_mid_length + tentacle_overlap -
               tentacle_mid_length + tentacle_overlap / 2)
	joint_y = limb_anchor_y
	self.joints.low_mid = love.physics.newRevoluteJoint( self.bodies.mid,
                                                         self.bodies.low_mid,
                                                         joint_x, joint_y )
	self.joints.low_mid:setLimitsEnabled( true )
	self.joints.low_mid:setLimits( -tentacle_joint_limit, tentacle_joint_limit )

	-- low joint
	joint_x = (limb_anchor_x -
               tentacle_high_length + tentacle_overlap -
               tentacle_high_mid_length + tentacle_overlap -
               tentacle_mid_length + tentacle_overlap -
               tentacle_low_mid_length + tentacle_overlap / 2)
	joint_y = limb_anchor_y
	self.joints.low = love.physics.newRevoluteJoint( self.bodies.low_mid,
                                                     self.bodies.low,
                                                     joint_x, joint_y )
	self.joints.low:setLimitsEnabled( true )
	self.joints.low:setLimits( -tentacle_joint_limit, tentacle_joint_limit )
 end

function Tentacle:setDrawParams( from_tex, from_tint_r, from_tint_g, from_tint_b,
                                 to_tex, to_tint_r, to_tint_g, to_tint_b )
   self.from_tex = from_tex
   self.from_tint_r = from_tint_r
   self.from_tint_g = from_tint_g
   self.from_tint_b = from_tint_b
   self.to_tex = to_tex
   self.to_tint_r = to_tint_r
   self.to_tint_g = to_tint_g
   self.to_tint_b = to_tint_b
end

function Tentacle:setFade( fade )
   self.fade = fade
end

function Tentacle:draw()
   local tx
   local quad
   local angle
   local shape
   local body
   local crop_width
   local crop_height
   local crop_x
   local crop_y
   local s
   local ox
   local oy

   -- high
   body        = self.bodies.high
   tx          = self.textures.high[self.from_tex]
   angle       = body:getAngle() + rad(180)
   s           = 1
   ox          = tx:getWidth() / 2 - 15
   oy          = tx:getHeight() / 2 - 0
   love.graphics.setColor( self.from_tint_r, self.from_tint_g, self.from_tint_b, 255 )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )
   tx          = self.textures.high[self.to_tex]
   love.graphics.setColor( self.to_tint_r, self.to_tint_g, self.to_tint_b, 255 * self.fade )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )

   -- high mid
   body        = self.bodies.high_mid
   tx          = self.textures.high_mid[self.from_tex]
   angle       = body:getAngle() + rad(180)
   s           = 1
   ox          = tx:getWidth() / 2  - 0
   oy          = tx:getHeight() / 2 + 3
   love.graphics.setColor( self.from_tint_r, self.from_tint_g, self.from_tint_b, 255 )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )
   tx          = self.textures.high_mid[self.to_tex]
   love.graphics.setColor( self.to_tint_r, self.to_tint_g, self.to_tint_b, 255 * self.fade )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )

   -- mid
   body        = self.bodies.mid
   tx          = self.textures.mid[self.from_tex]
   angle       = body:getAngle() + rad(180)
   s           = 0.9
   ox          = tx:getWidth() / 2  + 1
   oy          = tx:getHeight() / 2 + 3
   love.graphics.setColor( self.from_tint_r, self.from_tint_g, self.from_tint_b, 255 )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )
   tx          = self.textures.mid[self.to_tex]
   love.graphics.setColor( self.to_tint_r, self.to_tint_g, self.to_tint_b, 255 * self.fade )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )

   -- low mid
   body        = self.bodies.low_mid
   tx          = self.textures.low_mid[self.from_tex]
   angle       = body:getAngle() + rad(180)
   s           = 0.8
   ox          = tx:getWidth() / 2  - 8
   oy          = tx:getHeight() / 2 + 5
   love.graphics.setColor( self.from_tint_r, self.from_tint_g, self.from_tint_b, 255 )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )
   tx          = self.textures.low_mid[self.to_tex]
   love.graphics.setColor( self.to_tint_r, self.to_tint_g, self.to_tint_b, 255 * self.fade )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )

   -- low
   body        = self.bodies.low
   tx          = self.textures.low[self.from_tex]
   angle       = body:getAngle() + rad(180)
   s           = 0.75
   ox          = tx:getWidth() / 2  - 20
   oy          = tx:getHeight() / 2 + 8
   love.graphics.setColor( self.from_tint_r, self.from_tint_g, self.from_tint_b, 255 )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )
   tx          = self.textures.low[self.to_tex]
   love.graphics.setColor( self.to_tint_r, self.to_tint_g, self.to_tint_b, 255 * self.fade )
   love.graphics.draw( tx, body:getX(), body:getY(), angle, s, s, ox, oy )
end

function Tentacle:update( dt )
   -- I DO NOTHING
end
